using System;
using System.Collections;
using System.Web.UI.WebControls;
using Curriculum.Services;

namespace ConcursoCV.UI.Helper
{
	/// <summary>
	/// Summary description for ListHelper.
	/// </summary>
	public class ListHelper
	{
		public static void AddDefaultItem(ListControl ctrl)
		{
			ListItem item = new ListItem("- Seleccionar -", "0");
			ctrl.Items.Insert(0, item);
			item = null;
		}
		public static void FillDropDownList(ListControl ctrl, string dataValueField, string dataTextField,IList datasource, bool bAddSelect) 
		{
			ctrl.DataSource = datasource;
			ctrl.DataTextField = dataTextField;
			ctrl.DataValueField = dataValueField;
			ctrl.DataBind();

			if (TieneMasDeUnElemento(datasource) && (bAddSelect))
				AddDefaultItem( ctrl );			
		}
		public static void FillDropDownListLookup(ListControl ctrl, string type, bool addSelect) 
		{
			LookupTypeService serv = new LookupTypeService();
			IList list = serv.GetLookupsPorCodigo(type);
			Helper.ListHelper.FillDropDownList(ctrl,"id","descripcion",list,addSelect);
			ctrl.SelectedIndex=0;
		}	
		/// <summary>
		/// Rellena un dropdownlist con una lista de valores
		/// </summary>
		/// <param name="ctrl"></param>
		/// <param name="list"></param>
		public static void FillDropDownList(ListControl ctrl, IList list,  bool bAddSelect)
		{
			foreach(object value in list)
			{
				ctrl.Items.Add(value.ToString());				
			}
			if(bAddSelect)
				AddDefaultItem( ctrl );
			
			ctrl.SelectedIndex=0;
		}

		private static bool TieneMasDeUnElemento(IList lista)
		{
			return ((lista != null) && (lista.Count > 1));
		}
	}
}
